<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <style type="text/css">
        .layui-fluid {
            padding: 15px !important;
        }

        .my-card {
            width: 400px;
            padding: 15px;
            background-color: #fff;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-container my-card" style="width:400px;">
        <form action="" class="layui-form" id="passForm">
            <div class="layui-form-item">
                <label class="layui-form-label">旧密码</label>
                <div class="layui-input-block">
                    <input type="password" name="oldPwd"
                           lay-verify="required|pass"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-block">
                    <input type="password" name="newPwd" id="newPwd"
                           lay-verify="required|pass"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                    <input type="password" name="rePass" class="layui-input"
                           lay-verify="required|rePass"/>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-btn-container" style="text-align: center">
                    <button class="layui-btn"  style="margin: 0" lay-submit lay-filter="baseSubmit">
                        保存
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/js/core.util.js}"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        var basePath = CoreUtil.getContextPath();
        form.verify({
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ],
            //我们既支持上述数组的形式，也支持下述函数式的方式
            rePass: function (value) {
                var pass = $("#newPwd").val();
                if (pass !== value) {
                    return '两次输入的密码不一致';
                }
            }
        });
        form.on('submit(baseSubmit)', function (data) {
            CoreUtil.sendAjax(basePath + "/api/user/pwd", JSON.stringify(data.field), function (res) {
                layer.msg("密码已经变更请重新登录");
                top.window.location.href = basePath + "/index/login";
            }, "PUT", true);
            return false;
        })
    });
</script>
</html>